package controler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Clientes;
import model.ConnectionFabric;

/**
 * Servlet implementation class ServletConsultarCliente
 */
@WebServlet("/ServletConsultarCliente")
public class ServletConsultarCliente extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletConsultarCliente() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String codigo = request.getParameter("codigo");
		
		String sql;
		String nome =request.getParameter("nome");
		int func = 0;
		int cod = 0;
		
		
		if( codigo != ""){
			
		cod = Integer.parseInt(codigo);
		 
		sql = "select * from clientes where idCliente = ?";
		func = 1;
		}else{
		sql = "select * from clientes WHERE nome like ?";
		func = 2;
		}
                    
		         
                   

				    
				    try{
				    	
					    Connection con = ConnectionFabric.getConnection();	    	
					    PreparedStatement ps = con.prepareStatement(sql);
					    if (func == 1)
					    ps.setInt(1,cod); 
					    else
					    ps.setString(1,"%"+nome+"%");	
					    ResultSet rs = ps.executeQuery();	
					    List<Clientes> lista = new ArrayList<Clientes>(); 
					    
					    while(rs.next()){
					    Clientes cli = new Clientes();
					    cli.setIdcliente(rs.getInt("idcliente"));
					    cli.setNome(rs.getString("nome"));
					    cli.setTelefone(rs.getString("Telefone"));
					    cli.setEndereco(rs.getString("Endereco"));
					    cli.setBairro(rs.getString("Bairro"));
					    cli.setCep(rs.getString("cep"));
					    cli.setCidade(rs.getString("Cidade"));
					    cli.setEstado(rs.getString("Estado"));
					    cli.setCPF(rs.getString("cpf"));
					    cli.setCnpj(rs.getString("cnpj"));
					    cli.setContato(rs.getString("Contato"));
					    
					    lista.add(cli);    	
					    }
					    HttpSession sessao = request.getSession();
					    sessao.setAttribute("list", lista);  
					    RequestDispatcher rd = request.getRequestDispatcher("Cadclientes.jsp");  
					    rd.forward(request, response); 
					    }catch(Exception ex){	    	
					    	ex.printStackTrace();	
					                        }	
				    
				    
		                                          } 
				
		             

}
